package com.kd.controller;

import com.kd.common.IpUtils;
import com.kd.common.Result;
import com.kd.service.email.EmailCodeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/email")
public class EmailCodeController {

    @Autowired
    private EmailCodeService emailCodeService;

    /**
     * 发送验证码（带类型参数）
     */
    @PostMapping("/send")
    public Result<Void> sendEmail(
            @RequestParam String email,
            @RequestParam String type,
            HttpServletRequest request
    ) {

        String ip =IpUtils.getIp(request);
        boolean sent = emailCodeService.sendEmail(email,  type,ip);
        return sent ? Result.success("验证码发送成功")  : Result.fail(" 验证码发送失败");
    }

    /**
     * 验证验证码（带类型参数）
     */
    @PostMapping("/verify")
    public Result<Void> verifyEmailCode(
            @RequestParam String email,
            @RequestParam String code
    ) {
        boolean valid = emailCodeService.verifyEmailCode(email,  code);
        return valid ? Result.success("验证成功")  : Result.fail(" 验证码错误或已过期");
    }
}